// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-10-24 15:12:23
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Coordinate\CORD1CParser.cpp
 */

#include "Import/BDFImport/BulkData/Coordinate/CORD1CParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Coordinate/ICORD1CService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CORD1C);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CORD1C/CORD1C.md>
*/
// 以下需要修改
enum CORD1CKey : std::uint8_t
{
    CORD1RKEY_CIDA = 2,
    CORD1RKEY_G1A  = 3,  // Grid point identification number
    CORD1RKEY_G2A  = 4,
    CORD1RKEY_G3A  = 5,
    CORD1RKEY_CIDB = 6,
    CORD1RKEY_G1B  = 7,
    CORD1RKEY_G2B  = 8,
    CORD1RKEY_G3B  = 9,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCORD1C (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个 CORD1CData 类型的临时对象，用于存储解析出的坐标系数据
    SG::DataStructure::FEM::CORD1CData tempCORD1C1;
    SG::DataStructure::FEM::CORD1CData tempCORD1C2;

    // TODO(bug 001) m_dominId的正确填写
    tempCORD1C1.m_dominId = tempCORD1C2.m_dominId = 1;

    // 初始化当前字段的索引值
    int curIDKey = 1;

    // 标记是否有第二个坐标系定义
    bool isHaveSecond = false;

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 增加索引以便继续遍历
            continue;
        }

        // 根据索引值判断当前字段的含义，并解析字段内容
        switch (curIDKey)
        {
        case CORD1RKEY_CIDA:
            // 解析第一个坐标系的识别号
            tempCORD1C1.m_id = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G1A:
            // 解析第一个坐标系的第一个网格点
            tempCORD1C1.m_g1 = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G2A:
            // 解析第一个坐标系的第二个网格点
            tempCORD1C1.m_g2 = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G3A:
            // 解析第一个坐标系的第三个网格点
            tempCORD1C1.m_g3 = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_CIDB:
            // 检查是否存在第二个坐标系
            isHaveSecond = true;
            // 解析第二个坐标系的识别号
            tempCORD1C2.m_id = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G1B:
            // 解析第二个坐标系的第一个网格点
            tempCORD1C2.m_g1 = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G2B:
            // 解析第二个坐标系的第二个网格点
            tempCORD1C2.m_g2 = convertToI64 (curStrKey);
            break;
        case CORD1RKEY_G3B:
            // 解析第二个坐标系的第三个网格点
            tempCORD1C2.m_g3 = convertToI64 (curStrKey);
            break;
        default:
            // 处理其他无关字段
            break;
        }
        // 增加当前字段的索引以便处理下一个字段
        curIDKey++;
    }

    // 如果存在第二个坐标系定义，则将其添加到数据库
    if (isHaveSecond)
    {
        if (dbServiceFactory->get<ICORD1CService> ()->Add (tempCORD1C2) == Common::Status::STATUS_Success)
        {
            throw std::runtime_error ("Add CORD1C2 failed");
            return Common::Status::STATUS_Failure;
        }
    }

    // 将第一个坐标系的数据添加到数据库
    return dbServiceFactory->get<ICORD1CService> ()->Add (tempCORD1C1);
}